package cn.com.xinjinfu.pdoulcProduct.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.com.xinjinfu.pdoulcProduct.R;

/**
 * 页面内进度框，需要设置setOnClickListener为button设置监听
 * @author shi
 *
 */
public class LoadStatusBox extends RelativeLayout implements OnClickListener{
    private View ll_loadProgress;
    private ImageView progress;
    private OnClickListener  bnClick;
    private View ll_huojian;
    private AnimationDrawable mAnimation;
    public LoadStatusBox(Context context) {
        super(context);
    }
    public LoadStatusBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public LoadStatusBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public void setOnClickListener(OnClickListener l) {
        bnClick = l;
        super.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        loading();
    }
    /**
     * 开始加载用的方法
     */
    public void loading(){
        if(progress == null){
            ll_loadProgress = findViewById(R.id.ll_loadProgress);
            progress = (ImageView)findViewById(R.id.loadProgress);
            ll_huojian=findViewById(R.id.ll_no_net);
        }
        ll_huojian.setVisibility(View.GONE);
        ll_loadProgress.setVisibility(View.VISIBLE);

        mAnimation = (AnimationDrawable) progress.getBackground();
        // 为了防止在onCreate方法中只显示第一帧的解决方案之一
        progress.post(new Runnable() {
            @Override
            public void run() {
                mAnimation.start();

            }
        });
        bnClick.onClick(this);
    }
    public void failed(){
        ll_loadProgress.setVisibility(View.GONE);
        ll_huojian.setVisibility(View.VISIBLE);
    }
    public void success(){
        this.setVisibility(View.GONE);
    }
}